﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMS.IServices;
using PMS.Models.Base;
using PMS.Services;
using PMS.Utils;

namespace PMS.EasyUI.Areas.Base.Controllers
{
    public class BaseDictController : Controller
    {
        IBaseDictService _baseDictService = new BaseDictService();
        // GET: Base/BaseDict
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList()
        {
            string id = Request.Form["Id"] ?? "0";

            Hashtable ht = new Hashtable();
            ht.Add("ParentId", id);
            var data = _baseDictService.GetList(ht);

            return Json(new { total = data.Count, rows = data }, "text/html", JsonRequestBehavior.AllowGet);

        }


        public ActionResult Add()
        {
            string addType = Request.QueryString["AddType"] ?? "0";
            ViewBag.AddType = addType;
            BaseDict dict = new BaseDict();
            return View(dict);
        }


        [HttpPost]
        public ActionResult AddOrEdit(BaseDict dict)
        {
            //转换数据库不允许有null出现
            //if (dict.ParentId == 0)
            //{
            //    dict.ParentName = "";
            //    dict.Path = "";
            //    dict.PathName = "";
            //}

            if (dict.Id > 0)
            {
                ModelExtend<BaseDict>.SetModifyCommAll(dict);
                if (dict.ParentId == 0)
                {
                    dict.ParentName = "";
                    dict.Path = "";
                    dict.PathName = "";
                }
                int res = _baseDictService.Edit(dict);
                if (res > 0)
                {
                    return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                ModelExtend<BaseDict>.SetAddCommAll(dict);
                if (dict.ParentId == 0)
                {
                    dict.ParentName = "";
                    dict.Path = "";
                    dict.PathName = "";
                }
                //ParentId 的Model的IsLeaf更新为1
                int okOrNo = _baseDictService.AddEditParent(dict.ParentId);
                int res = _baseDictService.Add(dict);
                if (res > 0)
                {
                    return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
                }
            }

            return Json(Result.Error, "text/html", JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit()
        {
            string type = Request.QueryString["Type"] ?? "0";
            ViewBag.Type = type;
            int Id = (Request.QueryString["Id"] ?? "0").ToInt32();
            BaseDict dict = _baseDictService.Dtl(id: Id);
            return View(dict);
        }

        public ActionResult Delete()
        {
            int Id = (Request.Form["Id"] ?? "0").ToInt32();
            BaseDict dict = _baseDictService.Dtl(Id);
            int res = _baseDictService.Delete(Id, Session["UserId"].ToString());
            if (res > 0)
            {
                _baseDictService.DeleteEditParent(dict.ParentId);
                return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
            }

            return Json(Result.Error, "text/html", JsonRequestBehavior.AllowGet);

        }
    }
}